package com.wuchao.convert.controller;

import com.wuchao.convert.CommandManager;
import com.wuchao.convert.data.CommandTasker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author WuChao
 * @version 1.0
 * @date 2020/3/19 15:38
 */
@RestController
@RequestMapping("/control")
public class ControlController {

    @Autowired
    private CommandManager manager;


    /**
     * 开始播放 根据提供的rtsp流转码
     * @param rtsp
     * @return
     */
    @GetMapping("/start")
    public String start(String rtsp) {
//        CommandManager manager = new CommandManagerImpl();
        Map<String, String> map = new HashMap<String, String>();
        map.put("appName", UUID.randomUUID().toString());
        map.put("rtsp_transport", " -rtsp_transport tcp");
//        map.put("input", "rtsp://admin:a12345678@192.168.1.111:554/cam/realmonitor?channel=2&subtype=0");
       map.put("input", rtsp);
        map.put("output", "rtmp://127.0.0.1:1935/myapp/");
        map.put("codec", "h264");
        map.put("fmt", "flv");
        map.put("fps", "25");
        map.put("bv", "1500k");
        map.put("rs", "640x360");
        map.put("twoPart", "0");
        // 执行任务，id就是appName，如果执行失败返回为null
        String id = manager.start(map);
        System.out.println(id);
        // 通过id查询
        CommandTasker info = manager.query(id);
        System.out.println(info);
        // 查询全部
        Collection<CommandTasker> infoList = manager.queryAll();
        System.out.println(infoList);
        return id;
    }


    @GetMapping("/stop")
    public Boolean stop(String id) {
        boolean stop = manager.stop(id);
        return stop;
    }

    @PostMapping("replay")
    public String HuiFang(String rtsp, String startTime, String endTime
    ) {

        Map<String, String> map = new HashMap<String, String>();
        map.put("appName", UUID.randomUUID().toString());
        map.put("rtsp_transport", " -rtsp_transport tcp");
//        map.put("input", "rtsp://admin:a12345678@192.168.1.111:554/cam/realmonitor?channel=2&subtype=0");
        map.put("input", rtsp + "&" + startTime + "&" + endTime);
//        map.put("output", "rtmp://127.0.0.1:1935/myapp/");
        map.put("output", "rtmp://127.0.0.1:1935/myapp/");
        map.put("codec", "h264");
        map.put("bv", "15000k");
        map.put("fmt", "flv");
        map.put("fps", "25");
        map.put("rs", "640x360");
        map.put("twoPart", "0");
        // 执行任务，id就是appName，如果执行失败返回为null
        String id = manager.start(map);
        System.out.println(id);
        // 通过id查询
        CommandTasker info = manager.query(id);
        System.out.println(info);
        // 查询全部
        Collection<CommandTasker> infoList = manager.queryAll();
        System.out.println(infoList);
        return id;


    }


}
